<template>
  <div>
    <!-- 采用 Container 布局容器 -->
    <el-container class="main">
      <!-- 侧边栏 -->
      <el-aside width="auto">
        <!-- 采用 NavMenu 导航菜单 -->
        <!-- default-active 按照index的索引值高亮 -->
        <!-- background-color 背景色 -->
        <!-- text-color 字体颜色 -->
        <!-- active-text-color 当前激活菜单的文字颜色（仅支持 hex 格式） -->
        <!-- router 开启路由模式，开启后可以直接把路由地址写到index上面 -->
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          style="height:100%"
          :collapse="isCollapse"
          router
        >
          <el-menu-item index="/index">
            <!-- icon 图标 -->
            <i class="iconfont iconshouye"></i>
            <span slot="title">首页</span>
          </el-menu-item>

          <el-menu-item index="/index/kb-list">
            <i class="el-icon-medal"></i>
            <span slot="title">口碑列表</span>
          </el-menu-item>

          <el-menu-item index="/index/kb-add">
            <i class="el-icon-folder-add"></i>
            <span slot="title">添加口碑</span>
          </el-menu-item>

          <el-menu-item index="/index/kb-search">
            <i class="el-icon-search"></i>
            <span slot="title">查询口碑</span>
          </el-menu-item>

          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-s-promotion"></i>
              <span>文章中心</span>
            </template>

            <el-menu-item index="/index/article/list">文章列表</el-menu-item>
            <el-menu-item index="/index/article/add">添加文章</el-menu-item>
          </el-submenu>

          <el-menu-item index="/index/flink">
            <i class="el-icon-link"></i>
            <span slot="title">友情链接</span>
          </el-menu-item>

          <el-menu-item index="/index/user/info">
            <i class="el-icon-setting"></i>
            <span slot="title">用户设置</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-container>
        <!-- 头部 -->
        <el-header class="myheader">
          <div class="header-left">
            <i
              class="myicon"
              :class="{'el-icon-s-fold':!isCollapse,  'el-icon-s-unfold':isCollapse,}"
              @click="isCollapse = !isCollapse"
            ></i>
            北大青鸟中博软件学院
          </div>

          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                欢迎您：{{username}}
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="userinfo">用户信息</el-dropdown-item>
                <el-dropdown-item command="b">修改密码</el-dropdown-item>
                <el-dropdown-item command="loginout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>

        <!-- 主体 -->
        <el-main>
          <!-- 二级路由出口 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapse: false,
      username: "",
    };
  },
  created() {
    this.username = localStorage.getItem("username");
  },
  methods: {
    handleCommand(command) {
      if (command === "loginout") {
        // 采用 MessageBox 弹框
        this.$confirm("您真的要退出吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            // 清除token
            localStorage.removeItem("token");
            this.$router.replace("/login");
            this.$message({
              type: "success",
              message: "退出成功!",
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消",
            });
          });
      } 

      if (command == "userinfo") {
          this.$router.push('/index/user/info')
      }



    },
  },
};
</script>

<style scoped>
@import url("https://at.alicdn.com/t/font_2436691_qmhgc17qvi9.css");

.main {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

.iconshouye {
  margin-right: 6px;
  margin-left: 3px;
  font-size: 18px;
}

.myheader {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.myicon {
  font-size: 20px;
  cursor: pointer;
  position: relative;
  top: 1px;
}

.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
}

/* 下拉菜单的css */
.el-dropdown-link {
  cursor: pointer;
  color: #333;
}
.el-icon-arrow-down {
  font-size: 14px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>